/**
 * @(#) PassWordCheckMediator.as
 *
 * @date		2009. 02. 20.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	패스워드 체크 View - Mediator
 * 				급여조회를 위해 비밀번호를 체크하는 View
 */
package com.skt.common_hr.myprofile_sac.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.myprofile_sac.events.PassWordEvent;
	import com.skt.common_hr.myprofile_sac.model.PassWordCheckProxy;
	import com.skt.common_hr.myprofile_sac.view.components.PassWordCheckView;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class PassWordCheckMediator extends CommonMediator implements IMediator
	{
		public static const NAME:String = "PassWordCheckMediator"; 

		public static const VALID_PASSWORD:String					= "validPassword";
		public static const INVALID_PASSWORD:String					= "invalidPassword";

		public const bundle:String = "MyProfile";
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function PassWordCheckMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			//clearViewComponent();
 			loadPasswordInfo();
 			registerEventListener();
 		}
 		
 		public function execute():void
 		{
 			loadPasswordInfo();
 			clearViewComponent();
 		}
 		
 		private function registerEventListener():void
 		{
 			view.addEventListener(PassWordEvent.NEW_PASS_WORD, onNewPassSave);
 			view.addEventListener(PassWordCheckView.CHECK_PASSWORD, onCheckPass);
 			view.addEventListener(PassWordCheckView.CHANGE_PASSWORD_VIEW, onChangePassWordView);
 			view.addEventListener(PassWordCheckView.CHANGE_PASSWORD_CANCLE, onChangePassWordCancle);
 			view.addEventListener(PassWordCheckView.CHANGE_PASSWORD_INIT, onChangePassWordInit);
 			view.addEventListener(PassWordCheckView.CHANGE_PASSWORD_INIT_SAVE, onChangePassWordInitSave);
 			view.addEventListener(PassWordEvent.CHANGE_PASS_WORD, onChangePassWord);
 		}
 		
 		private function removeEventListener():void
 		{
 			view.removeEventListener(PassWordEvent.NEW_PASS_WORD, onNewPassSave);	
 			view.removeEventListener(PassWordCheckView.CHECK_PASSWORD, onCheckPass);
 			view.removeEventListener(PassWordCheckView.CHANGE_PASSWORD_VIEW, onChangePassWordView);
 			view.removeEventListener(PassWordCheckView.CHANGE_PASSWORD_CANCLE, onChangePassWordCancle);
 			view.removeEventListener(PassWordCheckView.CHANGE_PASSWORD_INIT, onChangePassWordInit);
 			view.removeEventListener(PassWordCheckView.CHANGE_PASSWORD_INIT_SAVE, onChangePassWordInitSave);
 			view.removeEventListener(PassWordEvent.CHANGE_PASS_WORD, onChangePassWord);
 		}
 		
 		public function clearViewComponent():void{
 			view.vsMain.selectedIndex = 1;
 			view.vsChangePass.selectedIndex = 0;

 			view.newPass.text = "";
 			view.reconfirmPass.text = "";
 			
 			view.priorPass.text = "";
 			
 			view.chgOldPass.text = "";
 			view.chgNewPass.text = "";
 			view.chgReconfirmPass.text = "";
 			view.initNewPass.text = "";
 			view.initReconfirmPass.text = "";
 			view.regno1.text = "";
 			view.regno2.text = "";
 		}
 		
 		
 		


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					PassWordCheckProxy.LOAD_PASSWORD_COMPLETE,
					PassWordCheckProxy.SAVE_NEWPASSWORD_COMPLETE,
					PassWordCheckProxy.SAVE_NEWPASSWORD_FAILED,
					PassWordCheckProxy.CHANGE_PASSWORD_COMPLETE,
					PassWordCheckProxy.CHANGE_PASSWORD_FAILED,
					PassWordCheckProxy.CHECK_PASSWORD_COMPLETE,
					PassWordCheckProxy.CHECK_PASSWORD_FAILED,
					
					PassWordCheckProxy.VALID_REGNO_COMPLETE,
					PassWordCheckProxy.VALID_REGNO_FAILED,
					PassWordCheckProxy.INIT_PASSWORD_COMPLETE,
					PassWordCheckProxy.INIT_PASSWORD_FAILED					
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case PassWordCheckProxy.LOAD_PASSWORD_COMPLETE:
					onLoadPassword(note.getBody() as PassWordCheckProxy);
					break;
				case PassWordCheckProxy.LOAD_PASSWORD_FAILED:
					onFailedLoadPassword(note.getBody() as PassWordCheckProxy);
					break;
				case PassWordCheckProxy.CHECK_PASSWORD_COMPLETE:
					onCheckPasswordComplete(note.getBody() as PassWordCheckProxy);
					break;
				case PassWordCheckProxy.CHECK_PASSWORD_FAILED:
					CommonAlert.showMessage(bundle, "ALERT_CHECK_PASSWORD", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;
				case PassWordCheckProxy.SAVE_NEWPASSWORD_COMPLETE:
					onCheckPasswordComplete(note.getBody() as PassWordCheckProxy);
					clearViewComponent();
					break;	
				case PassWordCheckProxy.SAVE_NEWPASSWORD_FAILED:
					CommonAlert.showMessage(bundle, "ALERT_SAVE_PASSWORD", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;	
				case PassWordCheckProxy.CHANGE_PASSWORD_COMPLETE:
					onCheckPasswordComplete(note.getBody() as PassWordCheckProxy);
					break;	
				case PassWordCheckProxy.CHANGE_PASSWORD_FAILED:
					CommonAlert.showMessage(bundle, "ALERT_SAVE_PASSWORD", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;	
				
				// 비번변경을 위한 주민번호 인증 확인	
				case PassWordCheckProxy.VALID_REGNO_COMPLETE:
					view.vsChangePass.selectedIndex = 3;		//비밀번호 확인 화면
					view.initNewPass.setFocus();
					break;
				case PassWordCheckProxy.VALID_REGNO_FAILED:
					CommonAlert.showMessage(bundle, "VALID_REGNO_COMPLETE", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;
					
				// 주민번호 초기화 	
				case PassWordCheckProxy.INIT_PASSWORD_COMPLETE:
					CommonAlert.showMessage(bundle, "ALERT_SAVE_PASSWORD", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;
				case PassWordCheckProxy.INIT_PASSWORD_FAILED:
					CommonAlert.showMessage(bundle, "INIT_PASSWORD_COMPLETE", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;
																									
										
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		/**
		 * 패스워드 정보가 정상적으로 로드되었을 때 
		 *    패스워드 없는 경우 패스워드 등록화면으로 이동
		 * 
		 * @param proxy:PassWordCheckProxy
		 * @return void
		 */
		private function onLoadPassword(proxy:PassWordCheckProxy):void
		{
			if(proxy.validateCode == 0) {
				view.vsMain.selectedIndex = 1;		//비밀번호 확인 화면
				view.vsChangePass.selectedIndex = 0;		//비밀번호 확인 화면
				var dateFormatter:DateFormatter = new DateFormatter();
				dateFormatter.formatString = "YYYY-MM-DD H:NN:SS";
				commonFacade.setStatusMessage("STATUS_LAST_LOGON", [dateFormatter.format(proxy.passwordInfo.LDATE)]);
			} 
			else {
				view.vsMain.selectedIndex = 0;		//신규비밀번호 지정화면
				CommonAlert.showMessage(bundle, "ALERT_INIT_PASSWORD", null, "ALERT_TITLE_INFO");
			}
		}
 		
 		
 		/**
		 * 패스워드 정보가 조회가 실패했을 경우
		 *    오류 메세지를 출력한다.
		 * 
		 * @param proxy:PassWordCheckProxy
		 * @return void
		 */
		private function onFailedLoadPassword(proxy:PassWordCheckProxy):void
 		{
			CommonAlert.showMessage(bundle, "ALERT_CHECK_PASSWORD", null, "ALERT_TITLE_SYSTEM_ERROR");
 		}


		private function onCheckPasswordComplete(proxy:PassWordCheckProxy):void
		{
			if(proxy.validateCode==0) {
				facade.sendNotification(VALID_PASSWORD);
				//setStatusMessage("적합한 비밀번호가 입력되었습니다. 급여정보를 조회합니다.");
			}
			else {
				CommonAlert.showMessage(bundle, proxy.validateText, null, "ALERT_TITLE_INFO");
			}
		}










 		
 		/****
 		 * 패스워드 등록 여부 확인
 		 * **/
 		private function loadPasswordInfo():void{
 			var proxy:PassWordCheckProxy = facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy;
 			proxy.loadPasswordInfo();
 		}
 		

  		/**
 		 * 신규 패스워드 등록
 		 * **/
 		private function onNewPassSave(event:PassWordEvent):void{
 			if(!passValidation(view.newPass.text, view.reconfirmPass.text)) return;
 			
 			if(view.newPass.text.length < 6 || view.newPass.text.length > 16){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_PASSWORD", null, "ALERT_TITLE_INVALID_DATA");
 				return;
 			}
 			var proxy:PassWordCheckProxy = facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy;
 			proxy.newPassSave(event.newPass, event.reconfirmPass);
 		}
 		
		/***
		 * 비밀번호 확인 로그인
		 * **/
		private function onCheckPass(event:Event):void
		{
			(facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy).checkPass(view.priorPass.text);
			//setStatusMessage("잠시만 기다리십시오...  입력된 비밀번호를 확인중입니다.");
		}
		
		/****
		 * 비밀번호 변경 화면으로 이동
		 * **/
		private function onChangePassWordView(event:Event):void{
			view.vsChangePass.selectedIndex = 1;
		}
		
		/***
		 * 비밀번호 확인 로그인 화면으로 이동
		 ***/
		private function onChangePassWordCancle(event:Event):void{
			view.vsChangePass.selectedIndex = 0;
			clearViewComponent();
		}
		
		
		/***
		 * 비밀번호 변경
		 ***/
		private function onChangePassWord(event:PassWordEvent):void{
			if(!passValidation(view.chgNewPass.text, view.chgReconfirmPass.text)) return;
			
			if(view.chgNewPass.text.length < 6 || view.chgNewPass.text.length > 16){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_PASSWORD", null, "ALERT_TITLE_INVALID_DATA");
 				return;
			}
			var proxy:PassWordCheckProxy = facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy;
 			proxy.changePass(event.newPass, event.reconfirmPass, event.oldPass);
		}

		
		/***
		 * 비밀번호 초기화를 위한 주민등록번호 인증
		 ***/
		private function onChangePassWordInit(event:Event):void{
			if(view.regno1.text.length != 6 || view.regno2.text.length != 7){
				Alert.show('주민등록번호를 확인해주세요.');
 				return;
			}
			var proxy:PassWordCheckProxy = facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy;
 			proxy.validRegno(view.regno1.text+view.regno2.text);
 			
		}	
		
		/***
		 * 비밀번호 초기화 저장.
		 ***/
		private function onChangePassWordInitSave(event:Event):void{
			if(!passValidation(view.initNewPass.text, view.initReconfirmPass.text)) return;
					
			var proxy:PassWordCheckProxy = facade.retrieveProxy(PassWordCheckProxy.NAME) as PassWordCheckProxy;
 			proxy.saveInitPass(view.initNewPass.text, view.initReconfirmPass.text);
		}
				
		
		
		//신규, 변경시 패스워드 벨리데이션 체크
 		private function passValidation(newPass:String, reChkPass:String):Boolean{
 			if(StringUtil.NVL(newPass) == "" || StringUtil.NVL(reChkPass) == ""){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD01", null, "ALERT_TITLE_INVALID_DATA");
 				return false;
 			}else if(newPass != reChkPass){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD02", null, "ALERT_TITLE_INVALID_DATA");
 				return false;
 			}else if(newPass.indexOf("<") > -1){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD03", null, "ALERT_TITLE_INVALID_DATA");
 				return false;
 			}else if(newPass.indexOf("?") > -1){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD04", null, "ALERT_TITLE_INVALID_DATA");
 				return false;
 			}else if(newPass.substr(0,1) == newPass.substr(1,1) == newPass.substr(2,1)){
				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD05", null, "ALERT_TITLE_INVALID_DATA");
 				return false;	
 			}else if(Variables.myPernr.indexOf(newPass.substr(0,3))>-1){
 				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD06", null, "ALERT_TITLE_INVALID_DATA");
 				return false;
 			}else if(newPass.toLocaleUpperCase().indexOf("SAP") >-1 || newPass.toLocaleUpperCase().indexOf("PASS") >0){
 				CommonAlert.showMessage(bundle, "ALERT_INVALID_NEW_PASSWORD07", null, "ALERT_TITLE_INVALID_DATA");
 				return false; 
 			}
 			return true; 
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():PassWordCheckView
        {
            return viewComponent as PassWordCheckView;
        }
	}
}